<?php

/*
 * 	Codificado: 02-Noviembre-2011, 18:09
	Estado: Ajustado_Modelo_Actual(X) Funcionando()
 */


class CursosIdiomas_Model extends CI_Model{

	private $table = 'CursosIdiomas';

	function _required($required, $data)
	{
		foreach($required as $field)
		if(!isset($data[$field])) return false;

		return true;
	}

	function Insertar($options = array()) {

		/*if ($this->_required(array('IdInvestigacion','IdInvestigador','TipoInvestigador'), $options))
		return FALSE ;*/

		$this->db->insert($this->table, $options);
		return $this->db->insert_id();

	}

	function Editar($options = array()) {

		if(!$this->_required(array('Ano','Idioma','Nivel'),$options))
		return FALSE;

		unset($options['submit'] );

		if(isset($options['Unidad']))
		$this->db->set('Unidad', $options['Unidad']);

		if(isset($options['NumeroInscritos']))
		$this->db->set('NumeroInscritos', $options['NumeroInscritos']);

		

			
		$this->db->where('Ano', $options['Ano']);
		$this->db->where('Idioma', $options['Idioma']);
		$this->db->where('Nivel', $options['Nivel']);
		$this->db->update($this->table, $options);

		return $this->db->affected_rows();
	}

	function Eliminar($options = array()) {

		$this->db->where('Ano', $options['Ano']);
		$this->db->where('Idioma', $options['Idioma']);
		$this->db->where('Nivel', $options['Nivel']);
		$this->db->delete($this->table);

		if($data['error'] = $this->db->_error_message());
		return $data;

	}

	function Ver($options = array()) {

		$this->db->where('Ano', $options['Ano']);
		$this->db->where('Idioma', $options['Idioma']);
		$this->db->where('Nivel', $options['Nivel']);
		$query = $this->db->get($this->table);

		if(isset($options['Ano']))
		return $query->row_array(0);

		return $query->result_array();
	}

	function Listar($options = array()) {

		$this->db->select('Ano,Idioma,Nivel,Unidad,NumeroInscritos' );

		if(isset($options['limit']) && isset($options['offset']))
		$this->db->limit($options['limit'], $options['offset']);
		else if(isset($options['limit']))
		$this->db->limit($options['limit']);
			
		$query = $this->db->get($this->table);
		if(isset($options['count']))
		return $query->num_rows();
			
		return $query->result_array();
	}


	function BuscarPorAno($options = array()) {
		if ($this->_required(array('Ano'), $options)) return FALSE ;

		$this->db->where('Ano', $options['Ano']);
		$query = $this->db->get($this->table);

		return $query->result_array();
	}

}
